package com.hs.controller;

import com.github.pagehelper.PageInfo;
import com.hs.entity.Center;
import com.hs.service.CenterService;
import com.hs.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @ClassName CenterController
 * @Description TODO
 * @Author qiuye
 * @Date 2025年07月31日 19:06
 * @Version 1.0
 */
// 公益中心管理
@RestController
@RequestMapping("center")
public class CenterController {
    @Autowired
    private CenterService centerService;
//    增加
    @PostMapping
    public R addCenter(@RequestBody Center center) {
        int add = centerService.add(center);
        if (add == 0){
            return R.fail();
        }else
            return R.ok();
    }
//    删除
    @DeleteMapping("{id}")
    public R deteleCenter(@PathVariable("id") int id) {
        int i = centerService.deteleByid(id);
        if (i == 0){
            return R.fail();
        } else{
            return R.ok();
        }
    }
    //  修改
    @PutMapping
    public R updateCenter(@RequestBody Center center){
        int update = centerService.update(center);
        if (update == 0){
            return R.fail();
        }else{
            return R.ok();
        }
    }
//    动态sql分页查询
    @GetMapping("page")
    public R getCenterPage(Center center ,@RequestParam(required = false,defaultValue = "1") int numPage,
                           @RequestParam(required = false,defaultValue = "2") int numSize){
        PageInfo<Center> centerPageInfo = centerService.selectByName(center,numPage,numSize);
        return R.ok(centerPageInfo);
    }
//    批量删除
    @DeleteMapping("deleteIds")
    public R deleteIds(Integer[] ids){
        int i = centerService.deleteByIds(ids);
        if (i == 0){
            return R.fail();
        }else {
            return R.ok();
        }
    }
//    根据id
    @GetMapping("{id}")
    public R getCenterById(@PathVariable("id") int id){
        Center center = centerService.selectById(id);
        return R.ok(center);
    }
}

